package com.example.panpan.customerManage.controller;

import com.example.common.utils.IMap;
import com.example.common.utils.PageView;
import com.example.common.utils.ReturnMapUtils;
import com.example.common.utils.SessionUtil;
import com.example.panpan.customerManage.service.CustomerService;
import com.github.pagehelper.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.List;
import java.util.Map;

@Controller
public class customerCon {
    @Autowired
    private CustomerService customerService;
    /**
     * 跳转客户管理页面
     * */
    @RequestMapping(value = "/customerCon/toPage/customerManageView")
    public String customerManageView(){
        return "/module/customerManage/customerManage";
    }
    /**
     * 获得客户列表
     * */
    @RequestMapping(value = "/customerCon/ajax/getCustomerList",method = RequestMethod.POST)
    @ResponseBody
    public Map getCustomerList(HttpServletRequest request){
        IMap params = new IMap(request);
        params.put("state","1");
        Page page = PageView.startPageForTable(params, false);
        List<IMap> customerList = customerService.getCustomerList(params);
        Map<String, Object> result = PageView.getResultForTable(page, customerList);
        return result;
    }
    /**
     * 添加新客户
     * */
    @RequestMapping(value = "/customerCon/toPage/customerAddView")
    public String customerAddView(){
        return "/module/customerManage/addCustomer";
    }
    @RequestMapping(value = "/customerCon/ajax/addCustomer",method = RequestMethod.POST)
    @ResponseBody
    public Map addProject(HttpServletRequest request){
        IMap params = new IMap(request);
        HttpSession session = request.getSession();
        String userId = SessionUtil.getLoginUserId(session);
        params.put("updateUser",userId);
        int i = customerService.addCustomer(params);
        if (i==1){
            return ReturnMapUtils.businessResult("0000","添加成功！(已自动创建账户，请用手机号登录)");
        }else {
            return ReturnMapUtils.businessResult("9999","服务器错误，添加失败！");
        }
    }
    /**
     * 信息修改
     * */
    @RequestMapping(value = "/customerCon/toPage/updCustomerView")
    public String updCustomerView(HttpServletRequest request,ModelMap modelMap){
        IMap params = new IMap(request);
        IMap thisCustomer = customerService.getCustomerInfo(params.getString("customerId"));
        modelMap.addAttribute("thisCustomer",thisCustomer);
        return "/module/customerManage/updCustomer";
    }
    @RequestMapping(value = "/customerCon/ajax/updCustomer",method = RequestMethod.POST)
    @ResponseBody
    public Map updCustomer(HttpServletRequest request){
        IMap params = new IMap(request);
        HttpSession session = request.getSession();
        String userId = SessionUtil.getLoginUserId(session);
        params.put("updateUser",userId);
        int i = customerService.updCustomer(params);
        if (i==1){
            return ReturnMapUtils.businessResult("0000","修改成功！");
        }else {
            return ReturnMapUtils.businessResult("9999","服务器错误，修改失败！");
        }
    }

    @RequestMapping(value = "/customerCon/ajax/delCustomer",method = RequestMethod.POST)
    @ResponseBody
    public Map delCustomer(HttpServletRequest request){
        IMap params = new IMap(request);
        HttpSession session = request.getSession();
        String userId = SessionUtil.getLoginUserId(session);
        params.put("updateUser",userId);
        int i = customerService.delCustomer(params);
        if (i==1){
            return ReturnMapUtils.businessResult("0000","删除成功！");
        }else {
            return ReturnMapUtils.businessResult("9999","服务器错误，删除失败！");
        }
    }
    /**
     * 跳转客户报表页面
     * */
    @RequestMapping(value = "/customerCon/toPage/customerReport")
    public String customerReport(){
        return "/module/report/customerReport";
    }
    /**
     * 获得客户列表
     * */
    @RequestMapping(value = "/customerCon/ajax/getCustomerListForReport",method = RequestMethod.POST)
    @ResponseBody
    public Map getCustomerListForReport(HttpServletRequest request){
        IMap params = new IMap(request);
        Page page = PageView.startPageForTable(params, false);
        List<IMap> customerList = customerService.getCustomerList(params);
        Map<String, Object> result = PageView.getResultForTable(page, customerList);
        return result;
    }
}
